package summary;

import java.util.PriorityQueue;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/7 15:16
 * https://leetcode-cn.com/problems/merge-k-sorted-lists/
 */
public class Title23 {

    class ListNode {
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode mergeKLists(ListNode[] lists) {
        ListNode newHead = new ListNode(-1);
        ListNode cur = newHead;
        PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>((a, b) -> Integer.compare(a.val, b.val));
        for (ListNode l : lists) {
            if (l != null) {
                priorityQueue.offer(l);
            }
        }
        while (!priorityQueue.isEmpty()) {
            ListNode poll = priorityQueue.poll();
            cur.next = poll;
            cur = cur.next;
            poll = poll.next;
            if (poll != null) {
                priorityQueue.offer(poll);
            }
        }
        return newHead.next;
    }

    public static void main(String[] args) {

    }
}
